﻿Imports DTO
Imports BUS
Public Class UserControlTraCuuSo
    Private _soTietKiem As New SoTietKiemCrt()

    Public Property SoTietKiem() As SoTietKiemCrt
        Get
            Return _soTietKiem
        End Get
        Set(ByVal value As SoTietKiemCrt)
            _soTietKiem = value
        End Set
    End Property

  
    Private Sub UserControlTraCuuSo_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        cbbLoaiSoTietKiem.DataSource = LoaiTietKiemBUS.LayLoaiTietKiem()
        cbbLoaiSoTietKiem.DisplayMember = "TenLoaiTietKiem"
        cbbLoaiSoTietKiem.ValueMember = "MaLoaiTietKiem"
    End Sub

   

    Private Sub chkLoaiSo_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkLoaiSo.CheckedChanged
        If chkLoaiSo.Checked = True Then
            cbbLoaiSoTietKiem.Enabled = True
        Else
            cbbLoaiSoTietKiem.Enabled = False
        End If
    End Sub

    Private Sub chkSoDu_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkSoDu.CheckedChanged
        If chkSoDu.Checked = True Then
            txtSoDuTu.Enabled = True
            txtSoDuDen.Enabled = True
        Else
            txtSoDuTu.Enabled = False
            txtSoDuDen.Enabled = False
        End If
    End Sub

    Private Sub btnTraCuu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTraCuu.Click
       
        Dim maso As Integer
        If txtMaSo.Text.Length > 0 And (Integer.TryParse(txtMaSo.Text, maso) = False Or maso <= 0) Then
            MessageBox.Show("Mã sổ không hợp lệ!")
            txtMaSo.Focus()
            Return
        End If


        Dim temp As Double
        Dim temp1 As Double
        If chkSoDu.Checked = True And (Double.TryParse(txtSoDuTu.Text, temp) = False Or temp < 0) Then
            MessageBox.Show("Số dư từ không hợp lệ!")
            txtSoDuTu.Focus()
            Return
        End If
        If chkSoDu.Checked = True And (Double.TryParse(txtSoDuDen.Text, temp1) = False Or temp1 < 0) Then
            MessageBox.Show("Số dư đến không hợp lệ!")
            txtSoDuDen.Focus()
            Return
        End If
        If temp > temp1 Then
            MessageBox.Show("Số dư từ lớn hơn số dư đến, không hợp lệ!")
            txtSoDuTu.Focus()
            Return
        End If
        If chkKhachHang.Checked = True And txtTenKhachHang.Text = "" Then
            MessageBox.Show("Chưa nhập tên khách hàng")
            txtTenKhachHang.Focus()
            Return
        End If
        Dim dtSTK As New DataTable()
        Try
            CapNhatTieuChuanTraCuu(True)
            Dim stkBUS As New SoTietKiemBUS()
            dtSTK = stkBUS.TimKiem(_soTietKiem)
            If dtSTK.Rows.Count <= 0 Then
                MessageBox.Show("Không tìm thấy dữ liệu")
                Return
            End If
            Dim frmDS As New frmDanhSachTraCuuSo()
            frmDS.DSSTK = dtSTK
            frmDS.ShowDialog()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
       
    End Sub

    Private Sub chkNgayMoSo_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkNgayMoSo.CheckedChanged
        If chkNgayMoSo.Checked = True Then
            dtpNgayMoSoTu.Enabled = True
            dtpNgayMoSoDen.Enabled = True
        Else
            dtpNgayMoSoTu.Enabled = False
            dtpNgayMoSoDen.Enabled = False
        End If

    End Sub
    ''''''''''''
    Private Sub CapNhatTieuChuanTraCuu(ByVal bUpdated As Boolean)
        If (bUpdated) Then
            If (txtMaSo.Text <> "") Then
                _soTietKiem.MaSo = Integer.Parse(txtMaSo.Text)
            Else
                _soTietKiem.MaSo = 0
            End If
            _soTietKiem.TenKhachHang = txtTenKhachHang.Text
            _soTietKiem.MaLoaiTietKiem = cbbLoaiSoTietKiem.SelectedValue
            If (_soTietKiem.ChkSoDu) Then
                _soTietKiem.SoDuTu = Double.Parse(txtSoDuTu.Text)
                _soTietKiem.SoDuDen = Double.Parse(txtSoDuDen.Text)
            Else
                _soTietKiem.SoDuTu = 0.0
                _soTietKiem.SoDuDen = 0.0
            End If

            _soTietKiem.NgayMoSoTu = dtpNgayMoSoTu.Value
            _soTietKiem.NgayMoSoDen = dtpNgayMoSoDen.Value

            _soTietKiem.ChkKhachHang = chkKhachHang.Checked
            _soTietKiem.ChkLoaiTietKiem = chkLoaiSo.Checked
            _soTietKiem.ChkSoDu = chkSoDu.Checked
            _soTietKiem.ChkNgayMoSo = chkNgayMoSo.Checked
        End If
    End Sub


 
    Private Sub chkKhachHang_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkKhachHang.CheckedChanged
        If chkKhachHang.Checked = True Then
            txtTenKhachHang.Enabled = True
        Else
            txtTenKhachHang.Enabled = False
        End If
    End Sub
End Class
